package com.youlai.boot.system.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.dto.EmissionReductionDTO;
import com.youlai.boot.system.model.entity.WasteType;
import com.youlai.boot.system.model.form.RecycleRequestForm;
import com.youlai.boot.system.model.query.RecycleRequestQuery;
import com.youlai.boot.system.model.vo.RecycleRequestVO;
import com.youlai.boot.system.service.RecycleRequestService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张雨润
 * @date 2025/2/23 20:50
 * @Description 垃圾回收--申请回收
 */
@Tag(name = "申请回收")
@RestController
@RequestMapping("/api/v1/recycle-request")
@RequiredArgsConstructor
public class RecycleRequestController {

    private final RecycleRequestService recycleRequestService;

    /**
     * (用户)查询申请回收分页数据
     * @param queryParams
     * @return
     */
    @Log(value = "查询申请回收分页数据", module = LogModuleEnum.RECYCLE)
    @PostMapping("/page")
    public Result<Map<String, List<RecycleRequestVO>>> getRecycleRequestPage(@RequestBody RecycleRequestQuery queryParams) {
        HashMap<String, List<RecycleRequestVO>> map = recycleRequestService.getPage(queryParams);
        return Result.success(map);
    }



    /**
     * （新增）用户发起的申请回收
     * @param recycleRequestForm
     * @return
     */
    @PostMapping("/user-init")
    public Result<Boolean> initUserRecycleRequest(@RequestBody RecycleRequestForm recycleRequestForm) {
        Boolean rs = recycleRequestService.initUserRecycleRequest(recycleRequestForm);
        return Result.success(rs);
    }

    /**
     * （修改）用户发起的申请回收
     * @param recycleRequestForm
     * @return
     */
    @PostMapping("/user-update")
    public Result<Boolean> updateUserRecycleRequest(@RequestBody RecycleRequestForm recycleRequestForm) {
        Boolean rs = recycleRequestService.updateUserRecycleRequest(recycleRequestForm);
        return Result.success(rs);
    }

    /**
     * （用户）申请取消请求
     * @param recycleRequestForm
     * @return
     */
    @PostMapping("/user-cancel")
    public Result<Boolean> applyCancelRequest(@RequestBody RecycleRequestForm recycleRequestForm){
        Boolean rs = recycleRequestService.applyCancelRequest(recycleRequestForm);
        return Result.success(rs);
    }

    /**
     * 查询请求表单数据
     * @param id
     * @return
     */
    @GetMapping("/{id}/form")
    public Result<RecycleRequestForm> getRecycleRequestForm(@PathVariable("id") Long id) {
        RecycleRequestForm recycleRequestForm = recycleRequestService.getRecycleRequestFormData(id);
        return Result.success(recycleRequestForm);
    }

    /**
     * 返回所有垃圾类型
     * @return
     */
    @GetMapping("/all-waste-type")
    public Result<List<WasteType>> getAllWasteType() {
        return Result.success(recycleRequestService.getAllWasteType());
    }


    /**
     * 首页统计Echarts 碳减排量/率
     * @param periodType 周期类型：
     *      0: 30天 （一个月）
     *      1:180天 （六个月）
     * @return
     */
    @GetMapping("/emission-stats")
    public Result<List<EmissionReductionDTO>> getEmissionStats(@RequestParam Integer periodType) {
        return Result.success(recycleRequestService.calculateReductions(periodType));
    }
}
